package com.whm.hrmanagement.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whm.common.utils.DateUtils;
import com.whm.hrmanagement.domain.OaZrsglryda;
import com.whm.hrmanagement.domain.dto.Extra;
import com.whm.system.domain.SysFileInfo;
import com.whm.system.mapper.SysFileInfoMapper;
import com.whm.system.service.ISysFileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.whm.hrmanagement.mapper.OaQpglqpmsMapper;
import com.whm.hrmanagement.domain.OaQpglqpms;
import com.whm.hrmanagement.service.IOaQpglqpmsService;

/**
 * hrmanagementService业务层处理
 * 
 * @author whm
 * @date 2024-02-04
 */
@Service
public class OaQpglqpmsServiceImpl implements IOaQpglqpmsService 
{
    @Autowired
    private OaQpglqpmsMapper oaQpglqpmsMapper;
    @Autowired
    private SysFileInfoMapper sysFileInfoMapper;
    @Autowired
    private ISysFileInfoService iSysFileInfoService;
    /**
     * 查询hrmanagement
     * 
     * @param id hrmanagement主键
     * @return hrmanagement
     */
    @Override
    public OaQpglqpms selectOaQpglqpmsById(Long id)
    {
        return oaQpglqpmsMapper.selectOaQpglqpmsById(id);
    }

    @Override
    public List<OaQpglqpms> selectOaQpglqpmsList(OaQpglqpms oaQpglqpms) {
        return oaQpglqpmsMapper.selectOaQpglqpmsList(oaQpglqpms);
    }

    /**
     * 查询hrmanagement列表
     * 
     * @param oaQpglqpms hrmanagement
     * @return hrmanagement
     */


    @Override
    public int insertOaQpglqpms(JSONObject data) {


        String jsonString = JSONObject.toJSONString(data.getJSONObject("form"));
        OaQpglqpms oaQpglqpms = JSONObject.parseObject(jsonString, OaQpglqpms.class);
        String jsonString2 = JSONArray.toJSONString(data.getJSONArray("Extra"));
        List<Extra> extras = JSONArray.parseArray(jsonString2,  Extra.class);
        Integer result = oaQpglqpmsMapper.insertOaQpglqpms(oaQpglqpms);
        QueryWrapper<SysFileInfo> qw = new QueryWrapper<>();

        for (Extra obj : extras) {
            System.out.println(obj.getFilePath());
            qw.lambda().eq(SysFileInfo::getFilePath,obj.getFilePath());

            SysFileInfo  sysFileInfo =  sysFileInfoMapper.selectOne(qw);
            sysFileInfo.setReltype("OaQpglqpms");
            sysFileInfo.setRelId(oaQpglqpms.getId());

            iSysFileInfoService.updateSysFileInfo(sysFileInfo);

        }

        return result;
    }

    @Override
    public int updateOaQpglqpms(JSONObject data) {

        String jsonString = JSONObject.toJSONString(data.getJSONObject("form"));
        OaQpglqpms oaQpglqpms = JSONObject.parseObject(jsonString, OaQpglqpms.class);
        String jsonString2 = JSONArray.toJSONString(data.getJSONArray("Extra"));
        List<Extra> extras = JSONArray.parseArray(jsonString2,  Extra.class);
        QueryWrapper<SysFileInfo> qw = new QueryWrapper<>();
        qw.lambda().eq(SysFileInfo::getRelId,oaQpglqpms.getId());
        List<SysFileInfo>  sysFileInfoList =  sysFileInfoMapper.selectList(qw);
        for (SysFileInfo obj : sysFileInfoList) {
            obj.setRelId(0L);
            iSysFileInfoService.updateSysFileInfo(obj);
        }

        for (Extra obj2 : extras) {
            QueryWrapper<SysFileInfo> qw2 = new QueryWrapper<>();
            qw2.lambda().eq(SysFileInfo::getFilePath,obj2.getFilePath());

            SysFileInfo  sysFileInfo2 =  sysFileInfoMapper.selectOne(qw2);;
            sysFileInfo2.setReltype("OaQpglqpms");
            sysFileInfo2.setRelId(oaQpglqpms.getId());
            iSysFileInfoService.updateSysFileInfo(sysFileInfo2);
        }
        oaQpglqpms.setUpdateTime(DateUtils.getNowDate());
        return oaQpglqpmsMapper.updateOaQpglqpms(oaQpglqpms);

    }

    /**
     * 新增hrmanagement
     * 
     * @param oaQpglqpms hrmanagement
     * @return 结果
     */


    /**
     * 修改hrmanagement
     * 
     * @param oaQpglqpms hrmanagement
     * @return 结果
     */


    /**
     * 批量删除hrmanagement
     * 
     * @param ids 需要删除的hrmanagement主键
     * @return 结果
     */
    @Override
    public int deleteOaQpglqpmsByIds(Long[] ids)
    {
        return oaQpglqpmsMapper.deleteOaQpglqpmsByIds(ids);
    }

    /**
     * 删除hrmanagement信息
     * 
     * @param id hrmanagement主键
     * @return 结果
     */
    @Override
    public int deleteOaQpglqpmsById(Long id)
    {
        return oaQpglqpmsMapper.deleteOaQpglqpmsById(id);
    }
}
